{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Traceback (most recent call last):\n",
      "  File \"C:\\Users\\00\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python311\\site-packages\\threadpool.py\", line 158, in run\n",
      "    result = request.callable(*request.args, **request.kwds)\n",
      "             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
      "  File \"C:\\Users\\00\\AppData\\Local\\Temp\\ipykernel_15636\\2596364022.py\", line 16, in Download\n",
      "    userName = tab.ele('@name=username')\n",
      "               ^^^^^^^^^^^^^^^^^^^^^^^^^\n",
      "  File \"C:\\Users\\00\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python311\\site-packages\\DrissionPage\\_pages\\chromium_base.py\", line 424, in ele\n",
      "    return self._ele(locator, timeout=timeout, index=index, method='ele()')\n",
      "           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
      "  File \"C:\\Users\\00\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python311\\site-packages\\DrissionPage\\_base\\base.py\", line 356, in _ele\n",
      "    r = self._find_elements(locator, timeout=timeout, index=index, raise_err=raise_err)\n",
      "        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
      "  File \"C:\\Users\\00\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python311\\site-packages\\DrissionPage\\_pages\\chromium_base.py\", line 497, in _find_elements\n",
      "    raise PageDisconnectedError\n",
      "DrissionPage.errors.PageDisconnectedError: 与页面的连接已断开。\n",
      "Traceback (most recent call last):\n",
      "  File \"C:\\Users\\00\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python311\\site-packages\\threadpool.py\", line 158, in run\n",
      "    result = request.callable(*request.args, **request.kwds)\n",
      "             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
      "  File \"C:\\Users\\00\\AppData\\Local\\Temp\\ipykernel_15636\\2596364022.py\", line 16, in Download\n",
      "    userName = tab.ele('@name=username')\n",
      "               ^^^^^^^^^^^^^^^^^^^^^^^^^\n",
      "  File \"C:\\Users\\00\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python311\\site-packages\\DrissionPage\\_pages\\chromium_base.py\", line 424, in ele\n",
      "    return self._ele(locator, timeout=timeout, index=index, method='ele()')\n",
      "           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
      "  File \"C:\\Users\\00\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python311\\site-packages\\DrissionPage\\_base\\base.py\", line 356, in _ele\n",
      "    r = self._find_elements(locator, timeout=timeout, index=index, raise_err=raise_err)\n",
      "        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
      "  File \"C:\\Users\\00\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python311\\site-packages\\DrissionPage\\_pages\\chromium_base.py\", line 497, in _find_elements\n",
      "    raise PageDisconnectedError\n",
      "DrissionPage.errors.PageDisconnectedError: 与页面的连接已断开。\n",
      "Traceback (most recent call last):\n",
      "  File \"C:\\Users\\00\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python311\\site-packages\\threadpool.py\", line 158, in run\n",
      "    result = request.callable(*request.args, **request.kwds)\n",
      "             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
      "  File \"C:\\Users\\00\\AppData\\Local\\Temp\\ipykernel_15636\\2596364022.py\", line 16, in Download\n",
      "    userName = tab.ele('@name=username')\n",
      "               ^^^^^^^^^^^^^^^^^^^^^^^^^\n",
      "  File \"C:\\Users\\00\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python311\\site-packages\\DrissionPage\\_pages\\chromium_base.py\", line 424, in ele\n",
      "    return self._ele(locator, timeout=timeout, index=index, method='ele()')\n",
      "           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
      "  File \"C:\\Users\\00\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python311\\site-packages\\DrissionPage\\_base\\base.py\", line 356, in _ele\n",
      "    r = self._find_elements(locator, timeout=timeout, index=index, raise_err=raise_err)\n",
      "        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
      "  File \"C:\\Users\\00\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python311\\site-packages\\DrissionPage\\_pages\\chromium_base.py\", line 497, in _find_elements\n",
      "    raise PageDisconnectedError\n",
      "DrissionPage.errors.PageDisconnectedError: 与页面的连接已断开。\n",
      "Traceback (most recent call last):\n",
      "  File \"C:\\Users\\00\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python311\\site-packages\\threadpool.py\", line 158, in run\n",
      "    result = request.callable(*request.args, **request.kwds)\n",
      "             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
      "  File \"C:\\Users\\00\\AppData\\Local\\Temp\\ipykernel_15636\\2596364022.py\", line 16, in Download\n",
      "    userName = tab.ele('@name=username')\n",
      "               ^^^^^^^^^^^^^^^^^^^^^^^^^\n",
      "  File \"C:\\Users\\00\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python311\\site-packages\\DrissionPage\\_pages\\chromium_base.py\", line 424, in ele\n",
      "    return self._ele(locator, timeout=timeout, index=index, method='ele()')\n",
      "           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
      "  File \"C:\\Users\\00\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python311\\site-packages\\DrissionPage\\_base\\base.py\", line 356, in _ele\n",
      "    r = self._find_elements(locator, timeout=timeout, index=index, raise_err=raise_err)\n",
      "        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
      "  File \"C:\\Users\\00\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python311\\site-packages\\DrissionPage\\_pages\\chromium_base.py\", line 497, in _find_elements\n",
      "    raise PageDisconnectedError\n",
      "DrissionPage.errors.PageDisconnectedError: 与页面的连接已断开。\n"
     ]
    },
    {
     "ename": "RuntimeError",
     "evalue": "此功能需显式设置下载路径（使用set.download_path()方法、配置对象或ini文件均可）。",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mRuntimeError\u001b[0m                              Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[1], line 98\u001b[0m\n\u001b[0;32m     94\u001b[0m     \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m下载完成\u001b[39m\u001b[38;5;124m'\u001b[39m)\n\u001b[0;32m     97\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;18m__name__\u001b[39m \u001b[38;5;241m==\u001b[39m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124m__main__\u001b[39m\u001b[38;5;124m'\u001b[39m:\n\u001b[1;32m---> 98\u001b[0m     \u001b[43mmain\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n",
      "Cell \u001b[1;32mIn[1], line 85\u001b[0m, in \u001b[0;36mmain\u001b[1;34m()\u001b[0m\n\u001b[0;32m     83\u001b[0m [pool\u001b[38;5;241m.\u001b[39mputRequest(req) \u001b[38;5;28;01mfor\u001b[39;00m req \u001b[38;5;129;01min\u001b[39;00m requests]\n\u001b[0;32m     84\u001b[0m pool\u001b[38;5;241m.\u001b[39mwait()\n\u001b[1;32m---> 85\u001b[0m \u001b[43mpage\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mwait\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mdownloads_done\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m     86\u001b[0m page\u001b[38;5;241m.\u001b[39mquit()\n\u001b[0;32m     87\u001b[0m file_name_s \u001b[38;5;241m=\u001b[39m os\u001b[38;5;241m.\u001b[39mpath\u001b[38;5;241m.\u001b[39mjoin(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mD:\u001b[39m\u001b[38;5;124m\\\u001b[39m\u001b[38;5;124mlz\u001b[39m\u001b[38;5;124m\\\u001b[39m\u001b[38;5;124m每日空调\u001b[39m\u001b[38;5;130;01m\\\\\u001b[39;00m\u001b[38;5;132;01m{\u001b[39;00myesterday\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m'\u001b[39m)\n",
      "File \u001b[1;32m~\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python311\\site-packages\\DrissionPage\\_units\\waiter.py:253\u001b[0m, in \u001b[0;36mTabWaiter.downloads_done\u001b[1;34m(self, timeout, cancel_if_timeout)\u001b[0m\n\u001b[0;32m    251\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mdownloads_done\u001b[39m(\u001b[38;5;28mself\u001b[39m, timeout\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mNone\u001b[39;00m, cancel_if_timeout\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m):\n\u001b[0;32m    252\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_owner\u001b[38;5;241m.\u001b[39mbrowser\u001b[38;5;241m.\u001b[39m_dl_mgr\u001b[38;5;241m.\u001b[39m_running:\n\u001b[1;32m--> 253\u001b[0m         \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mRuntimeError\u001b[39;00m(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m此功能需显式设置下载路径（使用set.download_path()方法、配置对象或ini文件均可）。\u001b[39m\u001b[38;5;124m'\u001b[39m)\n\u001b[0;32m    254\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m timeout:\n\u001b[0;32m    255\u001b[0m         \u001b[38;5;28;01mwhile\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_owner\u001b[38;5;241m.\u001b[39mbrowser\u001b[38;5;241m.\u001b[39m_dl_mgr\u001b[38;5;241m.\u001b[39mget_tab_missions(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_owner\u001b[38;5;241m.\u001b[39mtab_id):\n",
      "\u001b[1;31mRuntimeError\u001b[0m: 此功能需显式设置下载路径（使用set.download_path()方法、配置对象或ini文件均可）。"
     ]
    }
   ],
   "source": [
    "from DrissionPage import ChromiumPage\n",
    "import threadpool, os\n",
    "import datetime\n",
    "\n",
    "today = datetime.date.today()\n",
    "yesterday = today - datetime.timedelta(days=1)\n",
    "\n",
    "\n",
    "def Download(url_list):\n",
    "    '''下载空调数据'''\n",
    "    global today, yesterday\n",
    "\n",
    "    tab = url_list[0]\n",
    "    Air_conditioning = url_list[1]\n",
    "    tab.get('http://212.11.9.11:8077/static/#/login')\n",
    "    userName = tab.ele('@name=username')\n",
    "    userName.input('admin')\n",
    "    password = tab.ele('@name=password')\n",
    "    password.input('admin@123\\n')\n",
    "    tab.wait.load_start()\n",
    "    tab.get('http://212.11.9.11:8077/static/#/report/realStatistics')\n",
    "    tab.ele('自定义').click('js')\n",
    "    tab.ele(\n",
    "        'x:/html/body/div[1]/div/div[2]/div[2]/section/div[1]/div/div[2]/div[2]/div[3]/div[2]/div[1]/input'\n",
    "    ).input(str(yesterday) + ' 00:00:00\\n', clear=True)\n",
    "    tab.ele('确定').click('js')\n",
    "    tab.ele(\n",
    "        'x:/html/body/div[1]/div/div[2]/div[2]/section/div[1]/div/div[2]/div[2]/div[3]/div[2]/div[2]/input'\n",
    "    ).input(str(today) + ' 00:00:00\\n', clear=True)\n",
    "    tab.ele('确定').click('js')\n",
    "    for i in Air_conditioning:\n",
    "        search_for = tab.ele(\n",
    "            'x:/html/body/div/div/div[2]/div[2]/section/div[1]/div/div[1]/div/div[3]/div/input'\n",
    "        )\n",
    "        search_for.input(i + '\\n\\n', clear=True)\n",
    "        if len(tab.eles('楼')) == 2:\n",
    "            search_for.input(i + '\\n\\n', clear=True)\n",
    "        tab.ele('楼').click('js')\n",
    "        tab.ele('层').click('js')\n",
    "        machine_room = tab.ele('机房')\n",
    "        machine_room.click('js')\n",
    "        air_conditioning = tab.eles('空调')\n",
    "        for i in air_conditioning:\n",
    "            file_name = 'D:\\lz\\每日空调\\\\' + str(\n",
    "                yesterday) + '\\\\' + i.text + '.xlsx'\n",
    "            if os.path.exists(file_name):\n",
    "                # print(i.text + '已下载')\n",
    "                continue\n",
    "            i.click('js')\n",
    "            i.parent(2).ele('t:label').click()\n",
    "            while len(machine_room.parent(3).s_eles('t:div')) < 10:\n",
    "                pass\n",
    "            #下载\n",
    "            tab.set.when_download_file_exists('skip')\n",
    "            mission = tab('下载').click.to_download(\n",
    "                'D:\\lz\\每日空调\\\\' + str(yesterday), i.text,\n",
    "                by_js=True)  # 点击一个会触发下载的链接，同时设置下载路径和文件名\n",
    "            try:\n",
    "                mission.wait(180)  # 等待下载结束\n",
    "            except:\n",
    "                print(i.text + '下载有问题')\n",
    "                while not tab('下载').states.is_clickable:\n",
    "                    tab.wait(1)\n",
    "            i.parent(2).ele('t:label').click()\n",
    "\n",
    "\n",
    "def main():\n",
    "    '''主函数'''\n",
    "    global today, yesterday\n",
    "\n",
    "    page = ChromiumPage()\n",
    "    page.set.window.hide()\n",
    "    page_list = [page.new_tab() for i in range(4)]\n",
    "    url_list = [page.get_tab(i) for i in page_list]\n",
    "    air_conditioning = [['2-3空调#1', '2-3空调#2', '2-3空调#3', '2-3空调#4'],\n",
    "                        ['3-3空调#1', '3-3空调#2', '3-3空调#3'],\n",
    "                        ['3-4空调#1', '3-4空调#2', '3-4空调#3'],\n",
    "                        ['4-2空调#1', '4-2空调#2']]\n",
    "\n",
    "    url_list = [[url_list[i], air_conditioning[i]] for i in range(4)]\n",
    "    pool = threadpool.ThreadPool(4)\n",
    "    requests = threadpool.makeRequests(Download, url_list)\n",
    "    [pool.putRequest(req) for req in requests]\n",
    "    pool.wait()\n",
    "    page.wait.downloads_done()\n",
    "    page.quit()\n",
    "    file_name_s = os.path.join(f'D:\\lz\\每日空调\\\\{yesterday}')\n",
    "    while len(os.listdir(file_name_s)) < 24:\n",
    "        print('没有全部下载完成')\n",
    "        for i in os.listdir(file_name_s):\n",
    "            if '实时' in i:\n",
    "                os.remove(os.path.join(file_name_s, i))\n",
    "        main()\n",
    "    print('下载完成')\n",
    "\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    main()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Traceback (most recent call last):\n",
      "  File \"C:\\Users\\00\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python311\\site-packages\\threadpool.py\", line 158, in run\n",
      "    result = request.callable(*request.args, **request.kwds)\n",
      "             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
      "  File \"C:\\Users\\00\\AppData\\Local\\Temp\\ipykernel_15844\\4111038008.py\", line 42, in Download\n",
      "    tab.ele('楼').click('js')\n",
      "    ^^^^^^^^^^^^^^^^^^^\n",
      "  File \"C:\\Users\\00\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python311\\site-packages\\DrissionPage\\_elements\\none_element.py\", line 38, in __getattr__\n",
      "    raise ElementNotFoundError(None, self.method, self.args)\n",
      "DrissionPage.errors.ElementNotFoundError: \n",
      "没有找到元素。\n",
      "method: ele()\n",
      "args: {'locator': '楼', 'index': 1, 'timeout': 10}\n",
      "Traceback (most recent call last):\n",
      "  File \"C:\\Users\\00\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python311\\site-packages\\threadpool.py\", line 158, in run\n",
      "    result = request.callable(*request.args, **request.kwds)\n",
      "             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
      "  File \"C:\\Users\\00\\AppData\\Local\\Temp\\ipykernel_15844\\4111038008.py\", line 42, in Download\n",
      "    tab.ele('楼').click('js')\n",
      "    ^^^^^^^^^^^^^^^^^^^\n",
      "  File \"C:\\Users\\00\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python311\\site-packages\\DrissionPage\\_elements\\none_element.py\", line 38, in __getattr__\n",
      "    raise ElementNotFoundError(None, self.method, self.args)\n",
      "DrissionPage.errors.ElementNotFoundError: \n",
      "没有找到元素。\n",
      "method: ele()\n",
      "args: {'locator': '楼', 'index': 1, 'timeout': 10}\n",
      "Traceback (most recent call last):\n",
      "  File \"C:\\Users\\00\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python311\\site-packages\\threadpool.py\", line 158, in run\n",
      "    result = request.callable(*request.args, **request.kwds)\n",
      "             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
      "  File \"C:\\Users\\00\\AppData\\Local\\Temp\\ipykernel_15844\\4111038008.py\", line 42, in Download\n",
      "    tab.ele('楼').click('js')\n",
      "    ^^^^^^^^^^^^^^^^^^^\n",
      "  File \"C:\\Users\\00\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python311\\site-packages\\DrissionPage\\_elements\\none_element.py\", line 38, in __getattr__\n",
      "    raise ElementNotFoundError(None, self.method, self.args)\n",
      "DrissionPage.errors.ElementNotFoundError: \n",
      "没有找到元素。\n",
      "method: ele()\n",
      "args: {'locator': '楼', 'index': 1, 'timeout': 10}\n",
      "Traceback (most recent call last):\n",
      "  File \"C:\\Users\\00\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python311\\site-packages\\threadpool.py\", line 158, in run\n",
      "    result = request.callable(*request.args, **request.kwds)\n",
      "             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
      "  File \"C:\\Users\\00\\AppData\\Local\\Temp\\ipykernel_15844\\4111038008.py\", line 42, in Download\n",
      "    tab.ele('楼').click('js')\n",
      "    ^^^^^^^^^^^^^^^^^^^\n",
      "  File \"C:\\Users\\00\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python311\\site-packages\\DrissionPage\\_elements\\none_element.py\", line 38, in __getattr__\n",
      "    raise ElementNotFoundError(None, self.method, self.args)\n",
      "DrissionPage.errors.ElementNotFoundError: \n",
      "没有找到元素。\n",
      "method: ele()\n",
      "args: {'locator': '楼', 'index': 1, 'timeout': 10}\n"
     ]
    },
    {
     "ename": "RuntimeError",
     "evalue": "此功能需显式设置下载路径（使用set.download_path()方法、配置对象或ini文件均可）。",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mRuntimeError\u001b[0m                              Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[2], line 102\u001b[0m\n\u001b[0;32m     98\u001b[0m     \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m下载完成\u001b[39m\u001b[38;5;124m'\u001b[39m)\n\u001b[0;32m    101\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;18m__name__\u001b[39m \u001b[38;5;241m==\u001b[39m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124m__main__\u001b[39m\u001b[38;5;124m'\u001b[39m:\n\u001b[1;32m--> 102\u001b[0m     \u001b[43mmain\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n",
      "Cell \u001b[1;32mIn[2], line 89\u001b[0m, in \u001b[0;36mmain\u001b[1;34m()\u001b[0m\n\u001b[0;32m     87\u001b[0m [pool\u001b[38;5;241m.\u001b[39mputRequest(req) \u001b[38;5;28;01mfor\u001b[39;00m req \u001b[38;5;129;01min\u001b[39;00m requests]\n\u001b[0;32m     88\u001b[0m pool\u001b[38;5;241m.\u001b[39mwait()\n\u001b[1;32m---> 89\u001b[0m \u001b[43mpage\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mwait\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mdownloads_done\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m     90\u001b[0m page\u001b[38;5;241m.\u001b[39mquit()\n\u001b[0;32m     91\u001b[0m file_name_s \u001b[38;5;241m=\u001b[39m os\u001b[38;5;241m.\u001b[39mpath\u001b[38;5;241m.\u001b[39mjoin(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mD:\u001b[39m\u001b[38;5;124m\\\u001b[39m\u001b[38;5;124mlz\u001b[39m\u001b[38;5;124m\\\u001b[39m\u001b[38;5;124m每日空调\u001b[39m\u001b[38;5;130;01m\\\\\u001b[39;00m\u001b[38;5;132;01m{\u001b[39;00myesterday\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m'\u001b[39m)\n",
      "File \u001b[1;32m~\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python311\\site-packages\\DrissionPage\\_units\\waiter.py:68\u001b[0m, in \u001b[0;36mBrowserWaiter.downloads_done\u001b[1;34m(self, timeout, cancel_if_timeout)\u001b[0m\n\u001b[0;32m     66\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mdownloads_done\u001b[39m(\u001b[38;5;28mself\u001b[39m, timeout\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mNone\u001b[39;00m, cancel_if_timeout\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m):\n\u001b[0;32m     67\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_owner\u001b[38;5;241m.\u001b[39m_dl_mgr\u001b[38;5;241m.\u001b[39m_running:\n\u001b[1;32m---> 68\u001b[0m         \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mRuntimeError\u001b[39;00m(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m此功能需显式设置下载路径（使用set.download_path()方法、配置对象或ini文件均可）。\u001b[39m\u001b[38;5;124m'\u001b[39m)\n\u001b[0;32m     69\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m timeout:\n\u001b[0;32m     70\u001b[0m         \u001b[38;5;28;01mwhile\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_owner\u001b[38;5;241m.\u001b[39m_dl_mgr\u001b[38;5;241m.\u001b[39m_missions:\n",
      "\u001b[1;31mRuntimeError\u001b[0m: 此功能需显式设置下载路径（使用set.download_path()方法、配置对象或ini文件均可）。"
     ]
    }
   ],
   "source": [
    "from DrissionPage import Chromium, ChromiumOptions\n",
    "from DrissionPage.common import Settings\n",
    "import threadpool, os\n",
    "import datetime\n",
    "# 启用多例：\n",
    "Settings.singleton_tab_obj = False\n",
    "\n",
    "\n",
    "today = datetime.date.today()\n",
    "yesterday = today - datetime.timedelta(days=1)\n",
    "\n",
    "\n",
    "def Download(url_list):\n",
    "    '''下载空调数据'''\n",
    "    global today, yesterday\n",
    "\n",
    "    tab = url_list[0]\n",
    "    Air_conditioning = url_list[1]\n",
    "    tab.get('http://212.11.9.11:8077/static/#/login')\n",
    "    userName = tab.ele('@name=username')\n",
    "    userName.input('admin')\n",
    "    password = tab.ele('@name=password')\n",
    "    password.input('admin@123\\n')\n",
    "    tab.wait.load_start()\n",
    "    tab.get('http://212.11.9.11:8077/static/#/report/realStatistics')\n",
    "    tab.ele('自定义').click('js')\n",
    "    tab.ele(\n",
    "        'x:/html/body/div[1]/div/div[2]/div[2]/section/div[1]/div/div[2]/div[2]/div[3]/div[2]/div[1]/input'\n",
    "    ).input(str(yesterday) + ' 00:00:00\\n', clear=True)\n",
    "    tab.ele('确定').click('js')\n",
    "    tab.ele(\n",
    "        'x:/html/body/div[1]/div/div[2]/div[2]/section/div[1]/div/div[2]/div[2]/div[3]/div[2]/div[2]/input'\n",
    "    ).input(str(today) + ' 00:00:00\\n', clear=True)\n",
    "    tab.ele('确定').click('js')\n",
    "    for i in Air_conditioning:\n",
    "        search_for = tab.ele(\n",
    "            'x:/html/body/div/div/div[2]/div[2]/section/div[1]/div/div[1]/div/div[3]/div/input'\n",
    "        )\n",
    "        search_for.input(i + '\\n\\n', clear=True)\n",
    "        if len(tab.eles('楼')) == 2:\n",
    "            search_for.input(i + '\\n\\n', clear=True)\n",
    "        tab.ele('楼').click('js')\n",
    "        tab.ele('层').click('js')\n",
    "        machine_room = tab.ele('机房')\n",
    "        machine_room.click('js')\n",
    "        air_conditioning = tab.eles('空调')\n",
    "        for i in air_conditioning:\n",
    "            file_name = 'D:\\lz\\每日空调\\\\' + str(\n",
    "                yesterday) + '\\\\' + i.text + '.xlsx'\n",
    "            if os.path.exists(file_name):\n",
    "                # print(i.text + '已下载')\n",
    "                continue\n",
    "            i.click('js')\n",
    "            i.parent(2).ele('t:label').click()\n",
    "            while len(machine_room.parent(3).s_eles('t:div')) < 10:\n",
    "                pass\n",
    "            #下载\n",
    "            tab.set.when_download_file_exists('skip')\n",
    "            mission = tab('下载').click.to_download(\n",
    "                'D:\\lz\\每日空调\\\\' + str(yesterday), i.text,\n",
    "                by_js=True)  # 点击一个会触发下载的链接，同时设置下载路径和文件名\n",
    "            try:\n",
    "                mission.wait(180)  # 等待下载结束\n",
    "            except:\n",
    "                print(i.text + '下载有问题')\n",
    "                while not tab('下载').states.is_clickable:\n",
    "                    tab.wait(1)\n",
    "            i.parent(2).ele('t:label').click()\n",
    "\n",
    "\n",
    "def main():\n",
    "    '''主函数'''\n",
    "    global today, yesterday\n",
    "\n",
    "\n",
    "    co = ChromiumOptions().headless()\n",
    "    page = Chromium(co)\n",
    "    page_list = [page.new_tab() for i in range(4)]\n",
    "    air_conditioning = [['2-3空调#1', '2-3空调#2', '2-3空调#3', '2-3空调#4'],\n",
    "                        ['3-3空调#1', '3-3空调#2', '3-3空调#3'],\n",
    "                        ['3-4空调#1', '3-4空调#2', '3-4空调#3'],\n",
    "                        ['4-2空调#1', '4-2空调#2']]\n",
    "\n",
    "    url_list = [[page_list[i], air_conditioning[i]] for i in range(4)]\n",
    "    pool = threadpool.ThreadPool(4)\n",
    "    requests = threadpool.makeRequests(Download, url_list)\n",
    "    [pool.putRequest(req) for req in requests]\n",
    "    pool.wait()\n",
    "    page.wait.downloads_done()\n",
    "    page.quit()\n",
    "    file_name_s = os.path.join(f'D:\\lz\\每日空调\\\\{yesterday}')\n",
    "    while len(os.listdir(file_name_s)) < 24:\n",
    "        print('没有全部下载完成')\n",
    "        for i in os.listdir(file_name_s):\n",
    "            if '实时' in i:\n",
    "                os.remove(os.path.join(file_name_s, i))\n",
    "        main()\n",
    "    print('下载完成')\n",
    "\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    main()\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
